<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar
      class="page-nav-bar"
      title="个人信息"
      left-arrow
      @click-left="$router.back()"
    ></van-nav-bar>

    <input 
      type="file" 
      hidden 
      ref="file"
      @change="onFileChange"
    >

    <!-- 个人信息 -->
    <van-cell
      class="photo-cell" 
      title="头像" 
      is-link
      center
      @click="$refs.file.click()"
    >
      <van-image
        class="avatar"
        fit="cover"
        round
        :src="user.photo"
      ></van-image>
    </van-cell>
    <van-cell 
      title="昵称" 
      :value="user.name" 
      is-link
      @click="isUpdateNameShow = true"
    ></van-cell>
    <van-cell 
      title="性别" 
      :value="user.gender === 0 ? '男' : '女'" 
      is-link
      @click="isUpdateGenderShow = true"
    ></van-cell>
    <van-cell 
      title="生日" 
      :value="user.birthday" 
      is-link
      @click="isUpdateBrithdayShow = true"
    ></van-cell>

    <!-- 编辑昵称 -->
    <van-popup
      v-model="isUpdateNameShow"
      style="height:100%"
      position="bottom"
    >
      <update-name
        v-if="isUpdateNameShow"
        @close="isUpdateNameShow = false"
        v-model="user.name"
      ></update-name>
    </van-popup>

    <!-- 编辑性别 -->
    <van-popup
      v-model="isUpdateGenderShow"
      position="bottom"
    >
      <update-gender
        v-if="isUpdateGenderShow"
        v-model="user.gender"
        @close="isUpdateGenderShow = false"
      ></update-gender>
    </van-popup>

     <!-- 编辑生日 -->
     <van-popup
      v-model="isUpdateBrithdayShow"
      position="bottom"
    >
      <update-brithday
        v-if="isUpdateBrithdayShow"
        v-model="user.birthday"
        @close="isUpdateBrithdayShow = false"
      ></update-brithday>
    </van-popup>

     <!-- 编辑头像 -->
     <van-popup
      v-model="isUpdatePhotoShow"
      position="bottom"
      style="height: 100%"
    >
      <update-photo
        v-if="isUpdatePhotoShow"
        :img="img"
        @close="isUpdatePhotoShow = false"
        @update-photo="user.photo = $event"
      ></update-photo>
    </van-popup>
  </div>
</template>

<script>
import { getUserProfile } from "@/api/user"
import UpdateName from "./commponents/update-name.vue"
import UpdateGender from "./commponents/update-gender.vue"
import UpdateBrithday from "./commponents/update-brithday.vue"
import UpdatePhoto from "./commponents/update-photo.vue"

export default {
  name: 'UserProfile',
  components: {
    UpdateName,UpdateGender,UpdateBrithday,UpdatePhoto
  },
  data () {
    return {
      user: {}, //个人信息
      isUpdateNameShow: false,
      isUpdateGenderShow: false,
      isUpdateBrithdayShow: false,
      isUpdatePhotoShow: false,
      img: null //预览的图片
    }
  },
  created () {
    this.loadUserProfile()
  },
  methods: {
    async loadUserProfile () {
      try {
        const { data } = await getUserProfile()
        this.user = data.data
      } catch (err) {
        console.log(err);
        this.$toast('获取个人资料失败')
      }
    },
    onFileChange () {
      // 获取文件对象
      const file = this.$refs.file.files[0]
      // 基于文章对象获取 blob 数据
      this.img = window.URL.createObjectURL(file)
      // 打开弹出层
      this.isUpdatePhotoShow = true
      // file-input 如果选了同一个文件不会触发 change 事件
      // 解决办法就是每次使用完成，把他的value 清空
      this.$refs.file.value = ''
    }
  }
}
</script>

<style scoped lang="less">
.user-profile {
  .avatar {
    width: 60px;
    height: 60px;
  }
  .van-popup {
    background-color: #f5f7f9;
  }
}
</style>